﻿using System;
using System.Linq;
using Microsoft.SharePoint;
using Microsoft.SharePoint.WebControls;
using System.IO;
using ConsolidationTool.Common;
using System.Collections.Generic;
using ConsolidationTool.WebParts.ControlTemplates;
using System.Web;
using System.Web.Script.Serialization;
using System.Web.UI.WebControls;

namespace ConsolidationTool.WebParts.Layouts.CReport
{
    public partial class MonthYTDReport : FSReportBase
    {

        #region BaseOverride

        /*
         * 继承自FSReportBase 这东西可以获取到TopSelector的信息，然后生成一个CEntity对象，这里边有很多通用信息，比如选择的年月和单位等等。。（项目里面很多我做的页面只要上面有选择公司和时间的控件，我都会继承这个类）
         * 这文件的运行流程是： 
         * 1. 通过参数t到BRC列表获取相关的Mapping信息与表头信息
         * 2. 通过获取的信息中的Transfer字段来判断是否需要生成Mapping数据，如果不用生成那就是Mapping字段的数据（因为有些都是一样的mapping我就没有重复去写
         * 3. 通过mapping信息生成ARTReportRow 对象
         * 4. 通过生成的ARTReportRow到MappingLocator中去处理数据
         * 5. 处理完数据生成RQDataItem集合，这个东西有很多地方都是生成这个。这是一个结果对象。用于到前台绑定
         * 6. 
         * 参数说明
         * t：列表名字
         * template： 使用模板的名字，如果这个参数为空，就自动使用列表名字加前缀MonthYTD_去加载模板
         * reportType：报告的类型，在加载FieldTemplate文件的时候去过滤字段的，因为有些报表可能不会要那么多的字段
         * 
         */
        public override ControlTemplates.CommonEntitySelector TopSelector
        {
            get
            {
                return commonEntitySelector as ControlTemplates.CommonEntitySelector;
            }
        }

        public override Repeater BaseReport
        {
            get
            {
                return EQRepeater;
            }
        }

        public override void BeforLoad()
        {
            base.BeforLoad();

            title.Text = ListTitle;
        }
        #endregion

        #region Property

        public string ListTitle
        {
            get
            {
                return HttpUtility.UrlDecode(Request["t"]);
            }
        }
        public string TemplateName
        {
            get
            {
                return HttpUtility.UrlDecode(Request["template"]);
            }
        }
        public string ReportType
        {
            get
            {
                return HttpUtility.UrlDecode(Request["reportType"]);
            }
        }

        public string BudgetProfix
        {
            get
            {
                if (isMerge)
                    return "R_Budget_";
                else
                    return "Budget_";
            }
        }
        public string ForecastProfix
        {
            get
            {
                if (isMerge)
                    return "R_Forecast_";
                else
                    return "Forecast_";
            }
        }
        Dictionary<string, string> rgcDic = new Dictionary<string, string>();
        #endregion

        #region SupportFunction 

        private void Log(string p)
        {
            if (File.Exists("C:\\fields.txt"))
                using (StreamWriter sw = new StreamWriter("c:\\fields.txt"))
                {
                    sw.WriteLine(p);
                }
        }

        private void LoadRGC()
        {


            var list = SPContext.Current.Web.Lists["RGC"];
            int itemCount = list.Items.Count;
            for (int i = 0; i < itemCount; i++)
            {
                var item = list.Items[i];
                if (!rgcDic.ContainsKey(item["RGC"] + ""))
                    rgcDic.Add(item["RGC"] + "", item["Description"] + "");
            }

        }
        private string GetRGC(string p)
        {
            string t = string.Empty;
            rgcDic.ToList().ForEach(c =>
            {
                if (c.Value == p)
                    t = c.Key;
            });
            return t;
        }
        private string GetRGCDescription(string p)
        {
            if (rgcDic.ContainsKey(p))
                return rgcDic[p];
            return p;
        }

        string GetFieldTemplate()
        {
            string temp = string.Empty;
            using (StreamReader sr = new StreamReader(Server.MapPath("Actual/FieldTemplate.js")))
            {
                temp = sr.ReadToEnd();
            }
            return temp;
        }

        private string GetTemplateRow(out string rowdata, out bool transfer)
        {
            string temp = string.Empty;
            rowdata = string.Empty;
            transfer = true;

            var item = BusinessService.GetReportConfig(TemplateName);

            if (item.Count == 0)
                item = BusinessService.GetReportConfig("MonthYTD_" + ListTitle);

            if (item.Count == 0)
                item = BusinessService.GetReportConfig("MonthYTD"); //get general mapping

            if (item.Count > 0)
            {
                temp = item[0]["Mapping"] + "";
                rowdata = item[0]["AdditionOutput"] + "";
                bool.TryParse(item[0]["Transfer"] + "", out transfer);
            }


            return temp;
        }

        private List<string> GetEmptyValues()
        {
            var temp = new List<string>();
            for (int i = 0; i < 35; i++)
            {
                temp.Add("0");
            }
            temp[20] = "";
            return temp;
        }
        private List<string> GetEmptyValues(int length)
        {
            var temp = new List<string>();
            for (int i = 0; i < length; i++)
            {
                temp.Add("0");
            }
            return temp;
        }

        #endregion


        public override void GenerateReport_Click(object sender, EventArgs e)
        {
            RepHeader.ItemDataBound += new RepeaterItemEventHandler(RepHeader_ItemDataBound);
            var jss = new JavaScriptSerializer();
            string rowdata = string.Empty;
            bool transfer = true;
            string tempConfigRow = GetTemplateRow(out rowdata, out transfer);//Mapping
            if (tempConfigRow.IsNullOrEmpty()) return;

            var TableData = jss.Deserialize<List<ARTTableRow>>(rowdata);//列名

            var AllMapping = new List<ARTReportRow>();
            var AllFields = new List<RQDataItem>();
            MappingLocator ML = new MappingLocator(CEntity);

            var relationships = jss.Deserialize<List<relaClass>>(GetFieldTemplate()).FirstOrDefault(p => p.Title == ListTitle);
            relationships = relationships == null ? new relaClass() : relationships;
            int count = 15;
            if (transfer)
            {
                var allFields = relationships.Fields;
                if (!ReportType.IsNullOrEmpty())
                    allFields = allFields.Where(p => p.type == ReportType).ToList();
                string transferString = string.Empty;

                allFields.ForEach(p =>
                {
                    bool emptyRow = (p.ac.Trim().IsNullOrEmpty() && p.desc.Trim().IsNullOrEmpty()) || p.empty;
                    var tempRow = new ARTReportRow();
                    var rdi = new RQDataItem();

                    if (!emptyRow)
                    {
                        string temprowstr = GetBudgetForecastInfo(ListTitle, p.ac, tempConfigRow);
                        tempRow = jss.Deserialize<ARTReportRow>(temprowstr);
                        count = tempRow.fields.Count;

                        var newRow = new ARTReportRow()
                        {
                            identity = string.IsNullOrEmpty(p.field) ? p.ac.Trim() : p.field.Trim(),
                            title = p.desc.Trim(),
                            emptyRow = emptyRow,
                            bold = p.bold,
                            subrow = p.subrow,
                            digits = p.digits,
                            list = ListTitle,
                            fields = tempRow.fields,
                            //dengshuai
                            month=CEntity.Month
                        };

                        transferString += (temprowstr.Replace("_Title_", p.desc.Trim()).Replace("_Identity_", newRow.identity) + ",\r\n");

                        rdi = ML.GetMappingRowData(newRow);
                        rdi.identity = newRow.identity;
                        rdi.subrow = p.subrow;
                        rdi.unit = string.IsNullOrEmpty(p.unit) ? CEntity.Currency : Utility.UnitPlaceholderComplie(p.unit, commonEntitySelector);
                       // rdi.unit = string.IsNullOrEmpty(p.unit) ? newRow.unit : Utility.UnitPlaceholderComplie(p.unit, commonEntitySelector);
                        AllMapping.Add(newRow);
                    }
                    else
                    {
                        rdi.values = new string[count].ToList();
                        AllMapping.Add(new ARTReportRow() { emptyRow = true });
                        transferString += "{ emptyRow: true },\n";
                    }

                    rdi.acountcode = p.ac.Trim();
                    if (p.bold)
                        rdi.title = string.Format("<strong>{0}</strong>", p.desc);

                    AllFields.Add(rdi);
                });

                Log("[" + transferString+"]");
            }
            else
            {
                AllMapping = jss.Deserialize<List<ARTReportRow>>(tempConfigRow);

                AllMapping.ForEach(p =>
                {
                    bool emptyRow = (p.identity.IsNullOrEmpty() && p.title.IsNullOrEmpty()) || p.emptyRow;

                    p.list = p.list.IsNullOrEmpty() ? ListTitle : p.list;
                    p.Datasource = AllFields;
                    //dengshuai
                    p.month = CEntity.Month;
                    if (ListTitle == "DCFF18")
                    {
                        MakeDoneNewRow(p, TableData);
                    }

                    var rdi = ML.GetMappingRowData(p);

                    rdi.identity = p.identity;
                    rdi.subrow = p.subrow;
                    rdi.acountcode = p.identity;
                    if (!emptyRow)
                        rdi.unit = string.IsNullOrEmpty(p.unit) ? CEntity.Currency : Utility.UnitPlaceholderComplie(p.unit, commonEntitySelector);
                    if (p.bold)
                        rdi.title = string.Format("<strong>{0}</strong>", p.title);

                    AllFields.Add(rdi);
                });

            }
            bool hasSubRow = AllMapping.Exists(p => p.subrow);
            if (hasSubRow)
            {
                if (TemplateName == "ForecastTSBR" || TemplateName == "ForecastPSBR")
                    GetSubRowData_Other(AllMapping, AllFields);
                else
                    GetSubRowData(AllMapping, AllFields);
            }

            if (ListTitle == "DCFF17")
            {
                var newHeaderRow = new ARTTableRow();//创建表头、
                newHeaderRow.fields.Add(new ARTTableColumn() { title = "" });
                newHeaderRow.fields.Add(new ARTTableColumn() { title = "" });
                newHeaderRow.fields.Add(new ARTTableColumn() { title = "" });
                newHeaderRow.fields.Add(new ARTTableColumn() { title = CEntity.EngMonth + "-" + CEntity.Year });
                newHeaderRow.fields.Add(new ARTTableColumn() { title = CEntity.EngMonth + "-" + CEntity.Year });
                TableData.Add(newHeaderRow);
            }
            if (ListTitle == "DCFF16" || ListTitle == "DCFF18")
            {
                AllFields.ForEach(p => {
                    if (p.values != null && p.values.Count > 0) {
                        p.values.Add(p.values.Sum(a => BusinessService.decimalParse(a)).ToString("N0"));
                    } 
                });

                AllFields[AllFields.Count - 1].values[12] = AllFields[AllFields.Count - 1].values[11];
                AllFields[AllFields.Count - 3].values[12] = AllFields[AllFields.Count - 3].values[0];
            }

            RepHeader.DataSource = TableData;
            RepHeader.DataBind();

            BaseReport.DataSource = AllFields.Where(p=>!p.skip);
            BaseReport.DataBind();
        }


        private void MakeDoneNewRow(ARTReportRow newRow, List<ARTTableRow> tableData)
        {
            if (newRow.emptyRow || newRow.currentGroup || newRow.fields.Count >= 12) return;
            bool needCreateNewHeaderRow = tableData.Count == 1;
            var newHeaderRow = new ARTTableRow();//创建表头、
            newHeaderRow.fields.Add(new ARTTableColumn() { title = "" });
            newHeaderRow.fields.Add(new ARTTableColumn() { title = "" });
            newHeaderRow.fields.Add(new ARTTableColumn() { title = "" });
            newHeaderRow.fields.Add(new ARTTableColumn() { title = "Actual" });

            for (int i = 2; i <= CEntity.Month; i++)
            {
                var newField = newRow.fields[0].Clone() as ARTReportItem;
                newField.from.ForEach(p =>
                {
                    p.month = i;
                });
                newRow.fields.Add(newField);

                if (needCreateNewHeaderRow)
                    newHeaderRow.fields.Add(new ARTTableColumn() { title = "Actual" });
            }
            if (newRow.fields.Count == 12) return;
            var newForecastField = newRow.fields[0].Clone() as ARTReportItem;
            newForecastField.from.ForEach(p =>
            {
                string budget = "", budgetFixIdentity = "", forecast = "", forecastFixIdentity = "";
                p.identity = p.identity.IsNullOrEmpty() ? newRow.identity : p.identity;
                GetBudgetForecastIdentity(p.list, p.identity, out budget, out budgetFixIdentity, out forecast, out forecastFixIdentity);
                p.list = forecast;
                p.identity = forecastFixIdentity;
                p.month = CEntity.Month;
                p.field = "M_1";
                p.method = "CM";
            });
            newRow.fields.Add(newForecastField);
            if (needCreateNewHeaderRow)
                newHeaderRow.fields.Add(new ARTTableColumn() { title = "Forecast" });

            if (newRow.fields.Count == 12) return;
            var forecastField2 = newForecastField.Clone() as ARTReportItem;
            forecastField2.from.ForEach(p =>
            {
                p.field = "M_2";
            });
            newRow.fields.Add(forecastField2);
            if (needCreateNewHeaderRow)
                newHeaderRow.fields.Add(new ARTTableColumn() { title = "Forecast" });

            if (newRow.fields.Count == 12) return;

            for (int i = CEntity.Month + 3; i <= 12; i++)
            {
                if (newRow.fields.Count == 12) break;
                var newBudgetField = newRow.fields[0].Clone() as ARTReportItem;
                newBudgetField.from.ForEach(p =>
                {
                    p.identity = p.identity.IsNullOrEmpty() ? newRow.identity : p.identity;
                    string budget = "", budgetFixIdentity = "", forecast = "", forecastFixIdentity = "";
                    GetBudgetForecastIdentity(p.list, p.identity, out budget, out budgetFixIdentity, out forecast, out forecastFixIdentity);
                    p.list = budget;
                    p.identity = budgetFixIdentity;
                    p.month = i;
                });
                newRow.fields.Add(newBudgetField);
                if (needCreateNewHeaderRow)
                    newHeaderRow.fields.Add(new ARTTableColumn() { title = "Budget" });
            }

            if (needCreateNewHeaderRow)
                tableData.Add(newHeaderRow);
        }


        /// <summary>
        /// 这个获取子数据的原理就是
        /// Actual是通过父列的编号来做关联，到子表中取数据，然后合并到父行中
        /// Budget和forecast也差不多，只不过他们的没有子表，都是在一起。是在subrow对象中
        /// </summary>
        /// <param name="AllMapping">FileTemplate.js里的数据</param>
        /// <param name="rdis">绑定的数据</param>
        private void GetSubRowData(List<ARTReportRow> AllMapping, List<RQDataItem> rdis)
        {
            LoadRGC();
            string budget = "", budgetFixIdentity = "", forecast = "", forecastFixIdentity = "";
            GetBudgetForecastIdentity(ListTitle, "SV02020202", out budget, out budgetFixIdentity, out forecast, out forecastFixIdentity);

            var forecastYTD = GetForecastYTD(forecast);

            var budgetEntitys = new List<fsentity>();
            var budgetItem = BusinessService.GetBudgetItem(CEntity.Entity, CEntity.Year, budget);
            if (budgetItem != null)
                budgetEntitys = BusinessService.RefJsonData<fsentity>(budgetItem["BillData"] + "");

            var forecastEntitys = new List<fsentity>();
            var newSearch = CEntity.Clone() as SearchEntity;
            newSearch.SelectedDateTime = newSearch.SelectedDateTime.AddMonths(-1);
            var forecastItem = BusinessService.GetItem(CEntity.Entity, newSearch.SelectedDateTime.Year, newSearch.SelectedDateTime.Month, forecast);
            if (forecastItem != null)
                forecastEntitys = BusinessService.RefJsonData<fsentity>(forecastItem["BillData"] + "");

            AllMapping.Where(p => p.subrow).ToList().ForEach(p =>
            {
                GetBudgetForecastIdentity(ListTitle, p.identity, out budget, out budgetFixIdentity, out forecast, out forecastFixIdentity);

                var SubRow = new Dictionary<string, List<string>>();
                //Actaul N-1
                var n_1_items = BusinessService.GetSubItems(CEntity.Entity, (CEntity.Year - 1) + "", CEntity.Month + "", "Sub " + ListTitle, p.identity);
                if (n_1_items != null)
                {
                    for (int i = 0; i < n_1_items.Count; i++)
                    {

                        var listValues = GetEmptyValues();
                        var item = n_1_items[i];
                        listValues[0] = BusinessService.decimalParse(item["CM_QTY_" + p.identity] + "").ToString("N0");
                        listValues[1] = BusinessService.decimalParse(item["CM_T_" + p.identity] + "").ToString("N0");

                        var RGCDesc = GetRGCDescription(item["RGC"] + "");
                        if (!SubRow.ContainsKey(RGCDesc))
                            SubRow.Add(RGCDesc, listValues);

                    }
                }

                if (budgetEntitys.Count > 0)
                {
                    var budgetRow = budgetEntitys.FirstOrDefault(b => b.accountcode == budgetFixIdentity).subrow;
                    budgetRow.ForEach(a =>
                    {

                        var listValues = GetEmptyValues();
                        if (a.RGCDescription != null && SubRow.ContainsKey(a.RGCDescription))
                        {
                            listValues = SubRow[a.RGCDescription];
                        }

                        listValues[2] = BusinessService.GetObjectValue(a, CEntity.EngMonth + "_Qty").ToString("N0");
                        listValues[3] = BusinessService.GetObjectValue(a, CEntity.EngMonth + "_T").ToString("N0");

                        if (a.RGCDescription != null && !SubRow.ContainsKey(a.RGCDescription))
                            SubRow.Add(a.RGCDescription, listValues);
                    });

                }

                if (forecastEntitys.Count > 0)
                {
                    var forecastRow = forecastEntitys.FirstOrDefault(b => b.accountcode == forecastFixIdentity).subrow;
                    forecastRow.ForEach(a =>
                    {

                        var listValues = GetEmptyValues();
                        if (a.RGCDescription != null && SubRow.ContainsKey(a.RGCDescription))
                        {
                            listValues = SubRow[a.RGCDescription];
                        }

                        listValues[4] = BusinessService.GetObjectValue(a, "M_1_Qty").ToString("N0");
                        listValues[5] = BusinessService.GetObjectValue(a, "M_1_T").ToString("N0");

                        if (a.RGCDescription != null && !SubRow.ContainsKey(a.RGCDescription))
                            SubRow.Add(a.RGCDescription, listValues);
                    });
                }
                //Actaul
                var actualItems = BusinessService.GetSubItems(CEntity.Entity, CEntity.Year + "", CEntity.Month + "", "Sub " + ListTitle, p.identity);
                if (actualItems != null)
                {
                    for (int i = 0; i < actualItems.Count; i++)
                    {
                        var item = actualItems[i];
                        var listValues = GetEmptyValues();
                        string rgc = GetRGCDescription(item["RGC"] + "");

                        if (SubRow.ContainsKey(rgc))
                        {
                            listValues = SubRow[rgc];
                        }
                        listValues[6] = BusinessService.decimalParse(item["CM_QTY_" + p.identity] + "").ToString("N0");
                        listValues[7] = BusinessService.decimalParse(item["CM_T_" + p.identity] + "").ToString("N0");

                        if (!SubRow.ContainsKey(rgc))
                            SubRow.Add(GetRGCDescription(item["RGC"] + ""), listValues);
                    }
                }

                //n-1 ytd 
                decimal N_1_YTD_Value1 = 0.00M, N_1_YTD_Value2 = 0.00M;

                for (int i = 1; i <= CEntity.Month; i++)
                {
                    var N_1_YTD_Item = BusinessService.GetSubItems(CEntity.Entity, (CEntity.Year - 1) + "", i + "", "Sub " + ListTitle, p.identity);
                    if (N_1_YTD_Item != null)
                    {
                        for (int a = 0; a < N_1_YTD_Item.Count; a++)
                        {
                            var item = N_1_YTD_Item[a];
                            var listValues = GetEmptyValues();
                            string rgc = GetRGCDescription(item["RGC"] + "");

                            if (SubRow.ContainsKey(rgc))
                            {
                                listValues = SubRow[rgc];
                            }

                            listValues[21] = (BusinessService.decimalParse(listValues[21]) + BusinessService.decimalParse(item["CM_QTY_" + p.identity] + "")).ToString("N0");
                            listValues[22] = (BusinessService.decimalParse(listValues[22]) + BusinessService.decimalParse(item["CM_T_" + p.identity] + "")).ToString("N0");

                            if (!SubRow.ContainsKey(rgc))
                                SubRow.Add(rgc, listValues);
                        }
                    }
                }

                //Budget YTD
                if (budgetEntitys.Count > 0)
                {
                    var budgetRow = budgetEntitys.FirstOrDefault(b => b.accountcode == budgetFixIdentity).subrow;
                    budgetRow.ForEach(a =>
                    {
                        var listValues = GetEmptyValues();
                        if (a.RGCDescription != null && SubRow.ContainsKey(a.RGCDescription))
                        {
                            listValues = SubRow[a.RGCDescription];
                        }
                        decimal value1 = 0.00M;
                        decimal value2 = 0.00M;
                        for (int i = 1; i <= CEntity.Month; i++)
                        {
                            value1 += BusinessService.GetObjectValue(a, BusinessService.GetEngMonth(i) + "_Qty");
                            value2 += BusinessService.GetObjectValue(a, BusinessService.GetEngMonth(i) + "_T");
                        }

                        listValues[23] = value1.ToString("N0");
                        listValues[24] = value2.ToString("N0");

                        if (a.RGCDescription != null && !SubRow.ContainsKey(a.RGCDescription))
                            SubRow.Add(a.RGCDescription, listValues);
                    });
                }

                //Forecast YTD
                //if (forecastYTD.Count > 0)
                //{
                //    var forecastRow = forecastYTD.Where(b => b.accountcode == forecastFixIdentity);
                //    if (forecastRow != null)
                //    {
                //        forecastRow.ToList().ForEach(c =>
                //        {
                //            c.subrow.ForEach(e =>
                //            {

                //                var listValues = GetEmptyValues();
                //                if (e.RGCDescription != null && SubRow.ContainsKey(e.RGCDescription))
                //                    listValues = SubRow[e.RGCDescription];


                //                decimal value1 = 0.00M;
                //                decimal value2 = 0.00M;
                //                value1 += BusinessService.GetObjectValue(e, "M_1_Qty");
                //                value2 += BusinessService.GetObjectValue(e, "M_1_T");

                //                listValues[25] = BusinessService.decimalParse(listValues[25]) + value1 + "";
                //                listValues[26] = BusinessService.decimalParse(listValues[26]) + value2 + "";

                //                if (e.RGCDescription != null && !SubRow.ContainsKey(e.RGCDescription))
                //                    SubRow.Add(e.RGCDescription, listValues);
                //            });
                //        });
                //    }

                //}


                //actual ytd 
                decimal actual_YTD_Value1 = 0.00M, actual_YTD_Value2 = 0.00M;

                for (int i = 1; i <= CEntity.Month; i++)
                {
                    var actual_YTD_Item = BusinessService.GetSubItems(CEntity.Entity, CEntity.Year + "", i + "", "Sub " + ListTitle, p.identity);
                    if (actual_YTD_Item != null)
                    {
                        for (int a = 0; a < actual_YTD_Item.Count; a++)
                        {
                            var item = actual_YTD_Item[a];
                            var listValues = GetEmptyValues();
                            string rgc = GetRGCDescription(item["RGC"] + "");

                            if (SubRow.ContainsKey(rgc))
                            {
                                listValues = SubRow[rgc];
                            }
                     
                            listValues[25] = (BusinessService.decimalParse(listValues[25]) + BusinessService.decimalParse(item["CM_QTY_" + p.identity] + "")).ToString("N0");
                            listValues[26] = (BusinessService.decimalParse(listValues[26]) + BusinessService.decimalParse(item["CM_T_" + p.identity] + "")).ToString("N0");

                            if (!SubRow.ContainsKey(rgc))
                                SubRow.Add(rgc, listValues);
                        }
                    }
                }

                var rdiss = new List<RQDataItem>();
                SubRow.ToList().ForEach(c =>
                {
                    rdiss.Add(new RQDataItem()
                    {
                        title = GetRGC(c.Key),
                        unit = c.Key,
                        values = c.Value
                    });
                });

                //calc column
                rdiss.ForEach(c =>
                {
                    c.values[8] = Math.Round(BusinessService.decimalParse(c.values[6]) - BusinessService.decimalParse(c.values[0]), 0).ToString("N0");
                    c.values[9] = Math.Round(BusinessService.decimalParse(c.values[7]) - BusinessService.decimalParse(c.values[1]), 0).ToString("N0");
                    c.values[10] = Utilities.Division(c.values[6], c.values[0], false, 3);
                    c.values[11] = Utilities.Division(c.values[7], c.values[1], false, 3);

                    c.values[12] = Math.Round(BusinessService.decimalParse(c.values[6]) - BusinessService.decimalParse(c.values[2]), 0).ToString("N0");
                    c.values[13] = Math.Round(BusinessService.decimalParse(c.values[7]) - BusinessService.decimalParse(c.values[3]), 0).ToString("N0");
                    c.values[14] = Utilities.Division(c.values[6], c.values[2], false, 3);
                    c.values[15] = Utilities.Division(c.values[7], c.values[3], false, 3);

                    c.values[16] = Math.Round(BusinessService.decimalParse(c.values[6]) - BusinessService.decimalParse(c.values[4]), 0).ToString("N0");
                    c.values[17] = Math.Round(BusinessService.decimalParse(c.values[7]) - BusinessService.decimalParse(c.values[5]), 0).ToString("N0");
                    c.values[18] = Utilities.Division(c.values[6], c.values[4], false, 3);
                    c.values[19] = Utilities.Division(c.values[7], c.values[5], false, 3);
                    //ytd
                    c.values[27] = Math.Round(BusinessService.decimalParse(c.values[25]) - BusinessService.decimalParse(c.values[21]), 0).ToString("N0");
                    c.values[28] = Math.Round(BusinessService.decimalParse(c.values[26]) - BusinessService.decimalParse(c.values[22]), 0).ToString("N0");
                    c.values[29] = Utilities.Division(c.values[25], c.values[21], false, 3);
                    c.values[30] = Utilities.Division(c.values[26], c.values[22], false, 3);

                    c.values[31] = Math.Round(BusinessService.decimalParse(c.values[25]) - BusinessService.decimalParse(c.values[23]), 0).ToString("N0");
                    c.values[32] = Math.Round(BusinessService.decimalParse(c.values[26]) - BusinessService.decimalParse(c.values[24]), 0).ToString("N0");
                    c.values[33] = Utilities.Division(c.values[25], c.values[23], false, 3);
                    c.values[34] = Utilities.Division(c.values[26], c.values[24], false, 3);

                    //c.values[37] = Math.Round(BusinessService.decimalParse(c.values[27]) - BusinessService.decimalParse(c.values[25]), 2) + "";
                    //c.values[38] = Math.Round(BusinessService.decimalParse(c.values[28]) - BusinessService.decimalParse(c.values[26]), 2) + "";
                    //c.values[39] = Utilities.Division(c.values[27], c.values[25], false, 2);
                    //c.values[40] = Utilities.Division(c.values[28], c.values[26], false, 2);
                });

                int indexOfCurrent = rdis.IndexOf(rdis.FirstOrDefault(c => c.acountcode == p.identity));
                rdis.InsertRange(indexOfCurrent, rdiss);
            });
        }
        private void GetSubRowData_Other(List<ARTReportRow> AllMapping, List<RQDataItem> rdis)
        {
            LoadRGC();
            string budget = "", budgetFixIdentity = "", forecast = "", forecastFixIdentity = "";
            GetBudgetForecastIdentity(ListTitle, "SV02020202", out budget, out budgetFixIdentity, out forecast, out forecastFixIdentity);

            var forecastYTD = GetForecastYTD(forecast);//forecastYTD 从1月份到当前月份的总值

            var budgetEntitys = new List<fsentity>();
            var budgetItem = BusinessService.GetBudgetItem(CEntity.Entity, CEntity.Year, budget);//budget数据源
            if (budgetItem != null)
                budgetEntitys = BusinessService.RefJsonData<fsentity>(budgetItem["BillData"] + "");

            var forecastEntitys = new List<fsentity>();
            var forecastItem = BusinessService.GetItem(CEntity.Entity, CEntity.Year, CEntity.Month, forecast);//forecast数据源
            if (forecastItem != null)
                forecastEntitys = BusinessService.RefJsonData<fsentity>(forecastItem["BillData"] + "");

            AllMapping.Where(p => p.subrow).ToList().ForEach(p =>
            {
                GetBudgetForecastIdentity(ListTitle, p.identity, out budget, out budgetFixIdentity, out forecast, out forecastFixIdentity);

                var SubRow = new Dictionary<string, List<string>>();
                var listValues2 = GetEmptyValues(50);
                #region --n-1 ytd
                decimal N_1_YTD_Value1 = 0.00M, N_1_YTD_Value2 = 0.00M;
                string lastrgc = "";
                for (int i = 1; i <= CEntity.Month; i++)
                {
                    var N_1_YTD_Item = BusinessService.GetSubItems(CEntity.Entity, (CEntity.Year - 1) + "", i + "", "Sub " + ListTitle, p.identity);
                    if (N_1_YTD_Item != null)
                    {
                        for (int a = 0; a < N_1_YTD_Item.Count; a++)
                        {
                            var item = N_1_YTD_Item[a];
                            var listValues = GetEmptyValues(50);
                            string rgc = GetRGCDescription(item["RGC"] + "");

                            if (SubRow.ContainsKey(rgc))
                            {
                                listValues = SubRow[rgc];
                                N_1_YTD_Value1 = BusinessService.decimalParse(listValues[0]) + BusinessService.decimalParse(item["CM_QTY_" + p.identity] + "");
                                N_1_YTD_Value2 = BusinessService.decimalParse(listValues[1]) + BusinessService.decimalParse(item["CM_T_" + p.identity] + "");
                            }
                            else
                            {
                                if (lastrgc != item["RGC"].ToString())
                                {
                                    N_1_YTD_Value1 = 0.00M;
                                    N_1_YTD_Value2 = 0.00M;
                                }
                                N_1_YTD_Value1 += BusinessService.decimalParse(item["CM_QTY_" + p.identity] + "");
                                N_1_YTD_Value2 += BusinessService.decimalParse(item["CM_T_" + p.identity] + "");
                            }

                            listValues[0] = Convert.ToInt32(N_1_YTD_Value1).ToString("N0");
                            listValues[1] = Convert.ToInt32(N_1_YTD_Value2).ToString("N0");

                            if (!SubRow.ContainsKey(rgc))
                                SubRow.Add(rgc, listValues);
                            lastrgc = rgc;
                        }
                    }
                }
                #endregion

                #region --Budget YTD
                if (budgetEntitys.Count > 0)
                {
                    var budgetRow = budgetEntitys.FirstOrDefault(b => b.accountcode == budgetFixIdentity).subrow;
                    budgetRow.ForEach(a =>
                    {
                        var listValues = GetEmptyValues(50);
                        if (a.RGCDescription != null && SubRow.ContainsKey(a.RGCDescription))
                        {
                            listValues = SubRow[a.RGCDescription];
                        }
                        decimal value1 = 0.00M;
                        decimal value2 = 0.00M;
                        for (int i = 1; i <= CEntity.Month; i++)
                        {
                            value1 += BusinessService.GetObjectValue(a, BusinessService.GetEngMonth(i) + "_Qty");
                            value2 += BusinessService.GetObjectValue(a, BusinessService.GetEngMonth(i) + "_T");
                        }

                        listValues[2] = Convert.ToInt32(value1).ToString("N0");
                        listValues[3] = Convert.ToInt32(value2).ToString("N0");

                        if (a.RGCDescription != null && !SubRow.ContainsKey(a.RGCDescription))
                            SubRow.Add(a.RGCDescription, listValues);
                    });
                }
                #endregion

                #region --actual ytd
                decimal actual_YTD_Value1 = 0.00M, actual_YTD_Value2 = 0.00M;
                lastrgc = "";
                for (int i = 1; i <= CEntity.Month; i++)
                {
                    var actual_YTD_Item = BusinessService.GetSubItems(CEntity.Entity, CEntity.Year + "", i + "", "Sub " + ListTitle, p.identity);
                    if (actual_YTD_Item != null)
                    {
                        for (int a = 0; a < actual_YTD_Item.Count; a++)
                        {
                            var item = actual_YTD_Item[a];
                            var listValues = GetEmptyValues(50);
                            string rgc = GetRGCDescription(item["RGC"] + "");

                            if (SubRow.ContainsKey(rgc))
                            {
                                listValues = SubRow[rgc];
                                actual_YTD_Value1 = BusinessService.decimalParse(listValues[4]) + BusinessService.decimalParse(item["CM_QTY_" + p.identity] + "");
                                actual_YTD_Value2 = BusinessService.decimalParse(listValues[5]) + BusinessService.decimalParse(item["CM_T_" + p.identity] + "");
                            }
                            else
                            {
                                if (lastrgc != item["RGC"].ToString())
                                {
                                    actual_YTD_Value1 = 0.00M;
                                    actual_YTD_Value2 = 0.00M;
                                }
                                actual_YTD_Value1 += BusinessService.decimalParse(item["CM_QTY_" + p.identity] + "");
                                actual_YTD_Value2 += BusinessService.decimalParse(item["CM_T_" + p.identity] + "");
                            }

                            listValues[4] = actual_YTD_Value1.ToString("N0");
                            listValues[5] = actual_YTD_Value2.ToString("N0");

                            if (!SubRow.ContainsKey(rgc))
                                SubRow.Add(rgc, listValues);
                            lastrgc = item["RGC"].ToString();
                        }
                    }
                }
                #endregion

                #region --M+1
                //Actaul N-1

                var se = CEntity.Clone() as SearchEntity;
                se.SelectedDateTime = se.SelectedDateTime.AddYears(-1);
                se.SelectedDateTime = se.SelectedDateTime.AddMonths(1);

                var n_1_items = BusinessService.GetSubItems(CEntity.Entity, se.SelectedDateTime.Year+ "", se.SelectedDateTime.Month+ "", "Sub " + ListTitle, p.identity);
                if (n_1_items != null)
                {
                    for (int i = 0; i < n_1_items.Count; i++)
                    {
                        var listValues = GetEmptyValues(50);
                        var item = n_1_items[i];
                        string rgc = GetRGCDescription(item["RGC"] + "");
                        if (SubRow.ContainsKey(rgc))
                        {
                            listValues = SubRow[rgc];
                        }
                        listValues[10] = BusinessService.decimalParse(item["CM_QTY_" + p.identity].ToString()).ToString("N0");
                        listValues[11] = BusinessService.decimalParse(item["CM_T_" + p.identity].ToString()).ToString("N0");
                        if (GetRGCDescription(item["RGC"] + "") != null && !SubRow.ContainsKey(GetRGCDescription(item["RGC"] + "")))
                            SubRow.Add(GetRGCDescription(item["RGC"] + ""), listValues);
                    }
                }
                //Budget 
                se = CEntity.Clone() as SearchEntity; 
                se.SelectedDateTime = se.SelectedDateTime.AddMonths(1);

                budgetItem = BusinessService.GetBudgetItem(CEntity.Entity, se.SelectedDateTime.Year, budget);//budget数据源
                if (budgetItem != null)
                    budgetEntitys = BusinessService.RefJsonData<fsentity>(budgetItem["BillData"] + "");

                if (budgetEntitys.Count > 0)
                {
                    var budgetRow = budgetEntitys.FirstOrDefault(b => b.accountcode == budgetFixIdentity).subrow;
                    budgetRow.ForEach(a =>
                    {

                        var listValues = GetEmptyValues(50);
                        if (a.RGCDescription != null && SubRow.ContainsKey(a.RGCDescription))
                        {
                            listValues = SubRow[a.RGCDescription];
                        }

                        string engmonth1 = BusinessService.GetEngMonth(se.SelectedDateTime.Month );
                        string engmonth2 = BusinessService.GetEngMonth(se.SelectedDateTime.Month + 1);
                        string engmonth3 = BusinessService.GetEngMonth(se.SelectedDateTime.Month + 2);
                        listValues[12] = BusinessService.GetObjectValue(a, engmonth1 + "_Qty").ToString("N0");
                        listValues[13] = BusinessService.GetObjectValue(a, engmonth1 + "_T").ToString("N0");
                        listValues[22] = BusinessService.GetObjectValue(a, engmonth2 + "_Qty").ToString("N0");
                        listValues[23] = BusinessService.GetObjectValue(a, engmonth2 + "_T").ToString("N0");
                        listValues[32] = BusinessService.GetObjectValue(a, engmonth3 + "_Qty").ToString("N0");
                        listValues[33] = BusinessService.GetObjectValue(a, engmonth3 + "_T").ToString("N0");

                        if (a.RGCDescription != null && !SubRow.ContainsKey(a.RGCDescription))
                            SubRow.Add(a.RGCDescription, listValues);
                    });
                }
  
                //FCST
                if (forecastEntitys.Count > 0)
                {
                    var forecastRow = forecastEntitys.FirstOrDefault(b => b.accountcode == forecastFixIdentity).subrow;
                    forecastRow.ForEach(a =>
                    {

                        var listValues = GetEmptyValues(50);
                        if (a.RGCDescription != null && SubRow.ContainsKey(a.RGCDescription))
                        {
                            listValues = SubRow[a.RGCDescription];
                        }

                        listValues[14] = BusinessService.GetObjectValue(a, "M_1_Qty").ToString("N0");
                        listValues[15] = BusinessService.GetObjectValue(a, "M_1_T").ToString("N0");
                        listValues[24] = BusinessService.GetObjectValue(a, "M_2_Qty").ToString("N0");
                        listValues[25] = BusinessService.GetObjectValue(a, "M_2_T").ToString("N0");
                        listValues[34] = BusinessService.GetObjectValue(a, "M_3_Qty").ToString("N0");
                        listValues[35] = BusinessService.GetObjectValue(a, "M_3_T").ToString("N0");

                        if (a.RGCDescription != null && !SubRow.ContainsKey(a.RGCDescription))
                            SubRow.Add(a.RGCDescription, listValues);
                    });
                }
                #endregion

                #region --M+2
                //Actaul N-1
                se = CEntity.Clone() as SearchEntity;
                se.SelectedDateTime = se.SelectedDateTime.AddYears(-1);
                se.SelectedDateTime = se.SelectedDateTime.AddMonths(2);

                n_1_items = BusinessService.GetSubItems(CEntity.Entity, se.SelectedDateTime.Year + "", se.SelectedDateTime.Month   + "", "Sub " + ListTitle, p.identity);
                if (n_1_items != null)
                {
                    for (int i = 0; i < n_1_items.Count; i++)
                    {
                        var listValues = GetEmptyValues(50);
                        var item = n_1_items[i];
                        string rgc = GetRGCDescription(item["RGC"] + "");
                        if (SubRow.ContainsKey(rgc))
                        {
                            listValues = SubRow[rgc];
                        }
                        listValues[20] = BusinessService.decimalParse(item["CM_QTY_" + p.identity].ToString()).ToString("N0");
                        listValues[21] = BusinessService.decimalParse(item["CM_T_" + p.identity].ToString()).ToString("N0");
                        if (GetRGCDescription(item["RGC"] + "") != null && !SubRow.ContainsKey(GetRGCDescription(item["RGC"] + "")))
                            SubRow.Add(GetRGCDescription(item["RGC"] + ""), listValues);
                    }
                }
                #endregion

                #region --M+3
                //Actaul N-1

                se = CEntity.Clone() as SearchEntity;
                se.SelectedDateTime = se.SelectedDateTime.AddYears(-1);
                se.SelectedDateTime = se.SelectedDateTime.AddMonths(3);

                n_1_items = BusinessService.GetSubItems(CEntity.Entity, se.SelectedDateTime.Year + "", se.SelectedDateTime.Month + "", "Sub " + ListTitle, p.identity);
                if (n_1_items != null)
                {
                    for (int i = 0; i < n_1_items.Count; i++)
                    {
                        var listValues = GetEmptyValues(50);
                        var item = n_1_items[i];
                        string rgc = GetRGCDescription(item["RGC"] + "");
                        if (SubRow.ContainsKey(rgc))
                        {
                            listValues = SubRow[rgc];
                        }
                        listValues[30] = BusinessService.decimalParse(item["CM_QTY_" + p.identity].ToString()).ToString("N0");
                        listValues[31] = BusinessService.decimalParse(item["CM_T_" + p.identity].ToString()).ToString("N0");
                        if (GetRGCDescription(item["RGC"] + "") != null && !SubRow.ContainsKey(GetRGCDescription(item["RGC"] + "")))
                            SubRow.Add(GetRGCDescription(item["RGC"] + ""), listValues);
                    }
                }
                #endregion
                var rdiss = new List<RQDataItem>();
                SubRow.ToList().ForEach(c =>
                {
                    rdiss.Add(new RQDataItem()
                    {
                        title = GetRGC(c.Key),
                        unit=c.Key,
                        values = c.Value
                    });
                });

                #region --calc column
                rdiss.ForEach(c =>
                {
                    c.values[6] = Math.Round(BusinessService.decimalParse(c.values[4]) - BusinessService.decimalParse(c.values[0]), 2).ToString("N0");
                    c.values[7] = Math.Round(BusinessService.decimalParse(c.values[5]) - BusinessService.decimalParse(c.values[1]), 2).ToString("N0");
                    c.values[8] = Math.Round(BusinessService.decimalParse(c.values[4]) - BusinessService.decimalParse(c.values[2]), 2).ToString("N0");
                    c.values[9] = Math.Round(BusinessService.decimalParse(c.values[5]) - BusinessService.decimalParse(c.values[3]), 2).ToString("N0");

                    c.values[16] = Math.Round(BusinessService.decimalParse(c.values[14]) - BusinessService.decimalParse(c.values[10]), 2).ToString("N0");
                    c.values[17] = Math.Round(BusinessService.decimalParse(c.values[15]) - BusinessService.decimalParse(c.values[11]), 2).ToString("N0");
                    c.values[18] = Math.Round(BusinessService.decimalParse(c.values[14]) - BusinessService.decimalParse(c.values[12]), 2).ToString("N0");
                    c.values[19] = Math.Round(BusinessService.decimalParse(c.values[15]) - BusinessService.decimalParse(c.values[13]), 2).ToString("N0");

                    c.values[26] = Math.Round(BusinessService.decimalParse(c.values[24]) - BusinessService.decimalParse(c.values[20]), 2).ToString("N0");
                    c.values[27] = Math.Round(BusinessService.decimalParse(c.values[25]) - BusinessService.decimalParse(c.values[21]), 2).ToString("N0");
                    c.values[28] = Math.Round(BusinessService.decimalParse(c.values[24]) - BusinessService.decimalParse(c.values[22]), 2).ToString("N0");
                    c.values[29] = Math.Round(BusinessService.decimalParse(c.values[25]) - BusinessService.decimalParse(c.values[23]), 2).ToString("N0");

                    c.values[36] = Math.Round(BusinessService.decimalParse(c.values[34]) - BusinessService.decimalParse(c.values[30]), 2).ToString("N0");
                    c.values[37] = Math.Round(BusinessService.decimalParse(c.values[35]) - BusinessService.decimalParse(c.values[31]), 2).ToString("N0");
                    c.values[38] = Math.Round(BusinessService.decimalParse(c.values[34]) - BusinessService.decimalParse(c.values[32]), 2).ToString("N0");
                    c.values[39] = Math.Round(BusinessService.decimalParse(c.values[35]) - BusinessService.decimalParse(c.values[33]), 2).ToString("N0");

                    if (CEntity.Month == 10)
                    {
                        c.values[40] = Math.Round(BusinessService.decimalParse(c.values[0]) + BusinessService.decimalParse(c.values[10]) + BusinessService.decimalParse(c.values[20]), 2).ToString("N0");
                        c.values[41] = Math.Round(BusinessService.decimalParse(c.values[1]) + BusinessService.decimalParse(c.values[11]) + BusinessService.decimalParse(c.values[21]), 2).ToString("N0");
                        c.values[42] = Math.Round(BusinessService.decimalParse(c.values[2]) + BusinessService.decimalParse(c.values[12]) + BusinessService.decimalParse(c.values[22]), 2).ToString("N0");
                        c.values[43] = Math.Round(BusinessService.decimalParse(c.values[3]) + BusinessService.decimalParse(c.values[13]) + BusinessService.decimalParse(c.values[23]), 2).ToString("N0");
                        c.values[44] = Math.Round(BusinessService.decimalParse(c.values[4]) + BusinessService.decimalParse(c.values[14]) + BusinessService.decimalParse(c.values[24]), 2).ToString("N0");
                        c.values[45] = Math.Round(BusinessService.decimalParse(c.values[5]) + BusinessService.decimalParse(c.values[15]) + BusinessService.decimalParse(c.values[25]), 2).ToString("N0");
                        c.values[46] = Math.Round(BusinessService.decimalParse(c.values[6]) + BusinessService.decimalParse(c.values[16]) + BusinessService.decimalParse(c.values[26]), 2).ToString("N0");
                        c.values[47] = Math.Round(BusinessService.decimalParse(c.values[7]) + BusinessService.decimalParse(c.values[17]) + BusinessService.decimalParse(c.values[27]), 2).ToString("N0");
                        c.values[48] = Math.Round(BusinessService.decimalParse(c.values[8]) + BusinessService.decimalParse(c.values[18]) + BusinessService.decimalParse(c.values[28]), 2).ToString("N0");
                        c.values[49] = Math.Round(BusinessService.decimalParse(c.values[9]) + BusinessService.decimalParse(c.values[19]) + BusinessService.decimalParse(c.values[29]), 2).ToString("N0");
                    }
                    else if (CEntity.Month == 11)
                    {
                        c.values[40] = Math.Round(BusinessService.decimalParse(c.values[0]) + BusinessService.decimalParse(c.values[10]), 2).ToString("N0");
                        c.values[41] = Math.Round(BusinessService.decimalParse(c.values[1]) + BusinessService.decimalParse(c.values[11]), 2).ToString("N0");
                        c.values[42] = Math.Round(BusinessService.decimalParse(c.values[2]) + BusinessService.decimalParse(c.values[12]), 2).ToString("N0");
                        c.values[43] = Math.Round(BusinessService.decimalParse(c.values[3]) + BusinessService.decimalParse(c.values[13]), 2).ToString("N0");
                        c.values[44] = Math.Round(BusinessService.decimalParse(c.values[4]) + BusinessService.decimalParse(c.values[14]), 2).ToString("N0");
                        c.values[45] = Math.Round(BusinessService.decimalParse(c.values[5]) + BusinessService.decimalParse(c.values[15]), 2).ToString("N0");
                        c.values[46] = Math.Round(BusinessService.decimalParse(c.values[6]) + BusinessService.decimalParse(c.values[16]), 2).ToString("N0");
                        c.values[47] = Math.Round(BusinessService.decimalParse(c.values[7]) + BusinessService.decimalParse(c.values[17]), 2).ToString("N0");
                        c.values[48] = Math.Round(BusinessService.decimalParse(c.values[8]) + BusinessService.decimalParse(c.values[18]), 2).ToString("N0");
                        c.values[49] = Math.Round(BusinessService.decimalParse(c.values[9]) + BusinessService.decimalParse(c.values[19]), 2).ToString("N0");
                    }
                    else if (CEntity.Month == 12)
                    {
                /*        c.values[40] = Math.Round(BusinessService.decimalParse(c.values[0]), 2).ToString("N0");
                        c.values[41] = Math.Round(BusinessService.decimalParse(c.values[1]), 2).ToString("N0");
                        c.values[42] = Math.Round(BusinessService.decimalParse(c.values[2]), 2).ToString("N0");
                        c.values[43] = Math.Round(BusinessService.decimalParse(c.values[3]), 2).ToString("N0");
                        c.values[44] = Math.Round(BusinessService.decimalParse(c.values[4]), 2).ToString("N0");
                        c.values[45] = Math.Round(BusinessService.decimalParse(c.values[5]), 2).ToString("N0");
                        c.values[46] = Math.Round(BusinessService.decimalParse(c.values[6]), 2).ToString("N0");
                        c.values[47] = Math.Round(BusinessService.decimalParse(c.values[7]), 2).ToString("N0");
                        c.values[48] = Math.Round(BusinessService.decimalParse(c.values[8]), 2).ToString("N0");
                        c.values[49] = Math.Round(BusinessService.decimalParse(c.values[9]), 2).ToString("N0");*/

                        c.values[40] = Math.Round( BusinessService.decimalParse(c.values[10]) + BusinessService.decimalParse(c.values[20]) + BusinessService.decimalParse(c.values[30]), 2).ToString("N0");
                        c.values[41] = Math.Round( BusinessService.decimalParse(c.values[11]) + BusinessService.decimalParse(c.values[21]) + BusinessService.decimalParse(c.values[31]), 2).ToString("N0");
                        c.values[42] = Math.Round( BusinessService.decimalParse(c.values[12]) + BusinessService.decimalParse(c.values[22]) + BusinessService.decimalParse(c.values[32]), 2).ToString("N0");
                        c.values[43] = Math.Round( BusinessService.decimalParse(c.values[13]) + BusinessService.decimalParse(c.values[23]) + BusinessService.decimalParse(c.values[33]), 2).ToString("N0");
                        c.values[44] = Math.Round( BusinessService.decimalParse(c.values[14]) + BusinessService.decimalParse(c.values[24]) + BusinessService.decimalParse(c.values[34]), 2).ToString("N0");
                        c.values[45] = Math.Round( BusinessService.decimalParse(c.values[15]) + BusinessService.decimalParse(c.values[25]) + BusinessService.decimalParse(c.values[35]), 2).ToString("N0");
                        c.values[46] = Math.Round( BusinessService.decimalParse(c.values[16]) + BusinessService.decimalParse(c.values[26]) + BusinessService.decimalParse(c.values[36]), 2).ToString("N0");
                        c.values[47] = Math.Round( BusinessService.decimalParse(c.values[17]) + BusinessService.decimalParse(c.values[27]) + BusinessService.decimalParse(c.values[37]), 2).ToString("N0");
                        c.values[48] = Math.Round( BusinessService.decimalParse(c.values[18]) + BusinessService.decimalParse(c.values[28]) + BusinessService.decimalParse(c.values[38]), 2).ToString("N0");
                        c.values[49] = Math.Round( BusinessService.decimalParse(c.values[19]) + BusinessService.decimalParse(c.values[29]) + BusinessService.decimalParse(c.values[39]), 2).ToString("N0");
                    }
                    else
                    {
                        c.values[40] = Math.Round(BusinessService.decimalParse(c.values[0]) + BusinessService.decimalParse(c.values[10]) + BusinessService.decimalParse(c.values[20]) + BusinessService.decimalParse(c.values[30]), 2).ToString("N0");
                        c.values[41] = Math.Round(BusinessService.decimalParse(c.values[1]) + BusinessService.decimalParse(c.values[11]) + BusinessService.decimalParse(c.values[21]) + BusinessService.decimalParse(c.values[31]), 2).ToString("N0");
                        c.values[42] = Math.Round(BusinessService.decimalParse(c.values[2]) + BusinessService.decimalParse(c.values[12]) + BusinessService.decimalParse(c.values[22]) + BusinessService.decimalParse(c.values[32]), 2).ToString("N0");
                        c.values[43] = Math.Round(BusinessService.decimalParse(c.values[3]) + BusinessService.decimalParse(c.values[13]) + BusinessService.decimalParse(c.values[23]) + BusinessService.decimalParse(c.values[33]), 2).ToString("N0");
                        c.values[44] = Math.Round(BusinessService.decimalParse(c.values[4]) + BusinessService.decimalParse(c.values[14]) + BusinessService.decimalParse(c.values[24]) + BusinessService.decimalParse(c.values[34]), 2).ToString("N0");
                        c.values[45] = Math.Round(BusinessService.decimalParse(c.values[5]) + BusinessService.decimalParse(c.values[15]) + BusinessService.decimalParse(c.values[25]) + BusinessService.decimalParse(c.values[35]), 2).ToString("N0");
                        c.values[46] = Math.Round(BusinessService.decimalParse(c.values[6]) + BusinessService.decimalParse(c.values[16]) + BusinessService.decimalParse(c.values[26]) + BusinessService.decimalParse(c.values[36]), 2).ToString("N0");
                        c.values[47] = Math.Round(BusinessService.decimalParse(c.values[7]) + BusinessService.decimalParse(c.values[17]) + BusinessService.decimalParse(c.values[27]) + BusinessService.decimalParse(c.values[37]), 2).ToString("N0");
                        c.values[48] = Math.Round(BusinessService.decimalParse(c.values[8]) + BusinessService.decimalParse(c.values[18]) + BusinessService.decimalParse(c.values[28]) + BusinessService.decimalParse(c.values[38]), 2).ToString("N0");
                        c.values[49] = Math.Round(BusinessService.decimalParse(c.values[9]) + BusinessService.decimalParse(c.values[19]) + BusinessService.decimalParse(c.values[29]) + BusinessService.decimalParse(c.values[39]), 2).ToString("N0");
                    }
                    //c.values[37] = Math.Round(BusinessService.decimalParse(c.values[27]) - BusinessService.decimalParse(c.values[25]), 2) + "";
                    //c.values[38] = Math.Round(BusinessService.decimalParse(c.values[28]) - BusinessService.decimalParse(c.values[26]), 2) + "";
                    //c.values[39] = Utilities.Division(c.values[27], c.values[25], false, 2);
                    //c.values[40] = Utilities.Division(c.values[28], c.values[26], false, 2);
                });
                #endregion
                int indexOfCurrent = rdis.IndexOf(rdis.FirstOrDefault(c => c.acountcode == p.identity));
                rdis.InsertRange(indexOfCurrent, rdiss);
            });
        }

        private List<fsentity> GetForecastYTD(string title)
        {
            var allData = new List<List<fsentity>>();
            for (int i = 1; i <= CEntity.Month; i++)
            {
                var forecastItem = BusinessService.GetItem(CEntity.Entity, CEntity.Year, i, title);
                if (forecastItem != null)
                {
                    allData.Add(BusinessService.RefJsonData<fsentity>(forecastItem["BillData"] + ""));
                }
            }
            return KoEntityMerge(allData);
        } 
        private List<fsentity> KoEntityMerge(List<List<fsentity>> entitys)
        {

            var oneItem = new List<fsentity>();
            entitys.ForEach(p =>
            {
                oneItem = oneItem.Concat(p).ToList();
            });

            return oneItem;
        }
          

        public string GetBudgetForecastInfo(string listName, string actualIdentity, string tempConfigRow)
        {
            string budget = "", budgetFixIdentity = "", forecast = "", forecastFixIdentity = "";
            GetBudgetForecastIdentity(listName, actualIdentity, out budget, out budgetFixIdentity, out forecast, out forecastFixIdentity);

            return ReplaceTemplate(tempConfigRow, budget, budgetFixIdentity, forecast, forecastFixIdentity);
        }
        /// <summary>
        /// 这个函数是自动通过actual表名和编号来生成budget及forecast表名和编号的东西，很实用
        /// </summary>
        /// <param name="listName">actual表的名字</param>
        /// <param name="actualIdentity">actual的编号</param>
        public void GetBudgetForecastIdentity(string listName, string actualIdentity, out string budget, out string budgetFixIdentity, out string forecast, out string forecastFixIdentity)
        {
            string fix = "", identityFix = actualIdentity.Substring(0, 2);
            switch (listName)
            {
                case "HR":
                    fix = "HR";
                    break;
                case "Quality&Safety":
                    fix = "QS";
                    break;
                case "Product Sales By RGC":
                    fix = "PSBR";
                    break;
                case "Trade Sales By RGC":
                    fix = "TSBR";
                    break;
                case "Sales&Commission&Service":
                    fix = "SCS";
                    break;
                case "SBEW":
                    fix = "SBEW";
                    break;
                case "Gross Fixed Cost":
                    fix = "GRFC";
                    break;
                case "External Services":
                    identityFix = "ES";
                    fix = "ES";
                    break;
                case "FC_SG_A":
                    fix = "FSA";
                    break;
                case "Depreciation":
                    fix = "Depreciation";
                    break;
                case "P_L":
                    fix = "PL";
                    break;
                case "Direct Cash flow":
                    fix = "DCF";
                    break;
                case "CAPEX":
                    fix = "CAPEX";
                    break;
                case "Inventory":
                    fix = "Inventory";
                    break;
                case "Balance Sheet":
                    fix = "BS";
                    break;
                case "Indirect Cash flow":
                    fix = "ICF";
                    break;
            }
            budget = BudgetProfix + fix;
            forecast = ForecastProfix + fix;
            budgetFixIdentity = string.Format("{0}{1}{2}", identityFix, "03", actualIdentity.Substring(4));
            forecastFixIdentity = string.Format("{0}{1}{2}", identityFix, "04", actualIdentity.Substring(4));

            //shit to do 因为这个表的accountcode有问题，需要特殊处理。。。。
            if (actualIdentity == "SV01010300" && ListTitle == "Product Sales By RGC")
                budgetFixIdentity = "SV03020300";
        }
  
        private string ReplaceTemplate(string temp, string budget, string budgetIdentity, string forecast, string forecastIdentity)
        {
            return temp.Replace("Budget_List", budget).Replace("Budget_Identity", budgetIdentity).Replace("Forecast_List", forecast).Replace("Forecast_Identity", forecastIdentity);
        }
         
        void RepHeader_ItemDataBound(object sender, RepeaterItemEventArgs e)
        {
            var dataItem = e.Item.DataItem as ARTTableRow;
            Repeater rep = e.Item.FindControl("RPValue") as Repeater;
            rep.DataSource = dataItem.fields;
            rep.DataBind();
        }
    }
}
